package com.pro.service.imple;

import com.pro.mapper.JsCourseJournalMapper;
import com.pro.mapper.JsCourseMapper;
import com.pro.mapper.JsLogMapper;
import com.pro.pojo.JsCourse;
import com.pro.pojo.JsCourseJournal;
import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsLog;
import com.pro.service.JsCourseService;
import com.pro.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName:JsCourseServiceImple
 * Package:com.pro.service.imple
 * Description:
 *
 * @Date:2022/1/9 17:25
 * @Author: ZPT
 */
@Service
public class JsCourseServiceImple implements JsCourseService {
    @Autowired(required = false)
    private JsCourseMapper courseMapper;

    @Autowired(required = false)
    private JsLogMapper jsLogMapper;

    @Autowired(required = false)
    private JsCourseJournalMapper journalMapper;

    @Override
    public List<JsCourse> getCourse(Integer classId,String courseName,Double courseNprice) {
        return courseMapper.getCourse(classId,courseName,courseNprice);
    }

    @Override
    public Boolean updatecourseStatus(Integer courseId, Integer courseStatus,String updateDate,Integer updateId) {
        updateDate = DateUtil.tranDate(new Date());
        Boolean updatecourseStatus = courseMapper.updatecourseStatus(courseId, courseStatus, updateDate, updateId);
        return updatecourseStatus;
    }

    @Override
    public JsCourse getCourseById(Integer courseId,Integer userId) {
        JsCourse course = courseMapper.getCourseById(courseId);
        if (course!=null){
            course.setCommentNum(commentNum(course.getCourseId(),2));
            //添加课程操作日志
            JsLog jsLog = new JsLog("js_course","5",userId,new Date(),"查看课程详情",2);
            jsLogMapper.addLog(jsLog);
        }
        return course;
    }

    @Override
    public Integer commentNum(Integer courseId,Integer type) {
        return courseMapper.commentNum(courseId,type);
    }

    @Override
    public List<JsCourseRecord> getCourserRecord(Integer courseId,Integer type) {
        return courseMapper.getCourserRecord(courseId,type);
    }

    @Override
    public Boolean updateCourse(JsCourse course) {
        course.setUpdateDate(new Date());
        Boolean updateCourse = courseMapper.updateCourse(course);
        if (updateCourse){
            //添加课程操作日志
            JsLog jsLog = new JsLog("js_course","4",DateUtil.StrisInt(course.getUpdateId()),new Date(),"修改课程",2);
            jsLogMapper.addLog(jsLog);
            //添加历史价格详细
            //课程原价
            String coursePrice = course.getCoursePrice();
            //课程现价
            String courseNprice = course.getCourseNprice();
            if (!coursePrice.equals(courseNprice)){
                //创建历史价格详细对象
                JsCourseJournal jsCourseJournal = new JsCourseJournal(course.getCourseId(),course.getCourseName(),
                        course.getCourseStatus().toString(),course.getCourseCstatus().toString(),
                        DateUtil.StrisInt(course.getUpdateId()),new Date(),coursePrice,courseNprice,2);
                journalMapper.addJournal(jsCourseJournal);
            }
        }
        return updateCourse;
    }

    /**
     *
     * @author ZPT
     * @date 2022/1/11 9:10
     * @content :根据课程id查询历史价格
     */
    @Override
    public List<JsCourseJournal> getCourseJournal(Integer courseId,Integer type) {
        List<JsCourseJournal> courseJournalList = courseMapper.getCourseJournal(courseId,type);
        return courseJournalList;
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/11 9:57
     * @content :删除课程
     */
    @Override
    public Boolean deleteCourse(Integer courseId,Integer userId) {
        Boolean deleteCourse = courseMapper.deleteCourse(courseId);
        if (deleteCourse){
            //添加课程操作日志
            JsLog jsLog = new JsLog("js_course","3",userId,new Date(),"修改课程",2);
           jsLogMapper.addLog(jsLog);
        }
        return deleteCourse;
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/11 11:46
     * @content :课程高级检索
     */
    @Override
    public List<JsCourse> getRetrievalCourse(JsCourse course) {
        return courseMapper.getRetrievalCourse(course);
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/13 18:34
     * @content :添加课程
     */
    @Override
    public Boolean addCourse(JsCourse course) {
        course.setCreateDate(new Date());
        course.setCoursePrice(course.getCourseNprice());
        return courseMapper.addCourse(course);
    }
}
